package com.kkb.controller;

import com.kkb.constant.GlobalConst;
import com.kkb.domain.AjaxResult;
import com.kkb.page.QueryPageBean;
import com.kkb.page.TableDataInfo;
import com.kkb.pojo.TQuestion;
import com.kkb.pojo.TUser;
import com.kkb.service.QuestionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

//题库控制层
@Controller
public class QuestionController extends BaseController {
    @Autowired
    private QuestionService questionService;

    @PostMapping("/question/findListByPage")
    @ResponseBody
    public TableDataInfo findListByPage(@RequestBody QueryPageBean queryPageBean){
        //调用Service,，获取题目列表
        List<TQuestion> questionList = questionService.findListByPage(queryPageBean);
        return getDataTable(questionList);
     }

     //新增试题页面跳转
    @GetMapping("/questionEditor")
    public String questionEditor(int questionId, int is_classic) {
        return "/pages/questionEditor";
    }

    //添加基础题目
    @PostMapping("question/addOrUpdate")
    @ResponseBody
    public AjaxResult addOrUpdate(@RequestBody TQuestion question){
        try{
            //获取表单数据
            // 设置用户id
            TUser user = getSessionUser(GlobalConst.SESSION_KEY_USER);
            //为调试方便
            question.setUserId(user!=null?user.getId():1);
            logger.info("调用Service:question[]" + question);
            //调用 Service
            questionService.addOrUpdate(question);
            //响应
            return AjaxResult.success();
        }catch (Exception e){
            logger.error(e.getMessage());
            return AjaxResult.error();
        }
    }

    //分页获取精选题库列表
    @PostMapping("question/findClassicListByPage")
    @ResponseBody
    public TableDataInfo questionClassicList(@RequestBody QueryPageBean queryPageBean){
        //调用Service,，获取题目列表
        List<TQuestion> questionList = questionService.findClassicByPage(queryPageBean);
        return getDataTable(questionList);
    }

    //精选题库预览页面跳转
    @GetMapping("/questionPreview")
    public String questionPreview(int questionId,int is_classic,int is_examine) {
        return "pages/questionPreview";
    }

    @GetMapping("question/findById")
    @ResponseBody
    public AjaxResult findById(int questionId) {
        try {
            //调用Service方法
            logger.debug("查询问题，调用service，传入参数：" + questionId);
            TQuestion question = questionService.findById(questionId);
            return AjaxResult.success(question);
        } catch (Exception e) {
            logger.error(e.getMessage());
            return AjaxResult.error("查询失败" + e.getMessage());
        }

    }


}



